package controls {
	import Classes.pi_events;
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import cardsbody.*;
	
	import dialogs.pi_alert;
	
	import editor.pi_baseToolbar;
	import editor.pi_toolbarButton;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;

	public class pjf_toolbar extends pi_baseToolbar {
	    private var c_pay: pi_toolbarButton;
	    private var c_balance: pi_toolbarButton;
		private	var eyeFull: pi_toolbarButton;
		private	var link: pi_toolbarButton;
		private	var tofile: pi_toolbarButton;
		private var effect: pi_toolbarButton;
	    
		public function pjf_toolbar(parent:uif_component, startPos:int) {
			super(parent, startPos, 'bottom');
			
// Кнопка "Полный размер"			
			eyeFull = new pi_toolbarButton(this, pi_images.c_eye, pi_data.FULLVIEWCARD, function(e: MouseEvent): void {
				app.dispatchEvent(new Event(pi_events.EYERELEASE));
			}, 72);

/*			if (!pj.userData.hasOwnProperty('anonimus')) {
	// Кнопка "Входящие"
				var inbox_button: pi_toolbarButton = new pi_toolbarButton(this, pi_images.c_inbox, pi_data.INBOX, function(e: MouseEvent): void {
					if (!inbox_button.disable) app.dispatchEvent(new Event(pi_events.INBOXRELEASE));
				}, 85);
				inbox_button.disable = true;
				
				app.addEventListener(pi_events.CHECKBOX, function(e: ObjectEvent): void {
					inbox_button.disable  = false;//parseInt(e.object.inCount) == 0;
					outbox_button.disable = false;//parseInt(e.object.isOut) == 0;
					if (parseInt(e.object.receiveCount) < parseInt(e.object.inCount)) inbox_button.blink();
				});
	
	// Кнопка "Исходящие"
				var outbox_button: pi_toolbarButton = new pi_toolbarButton(this, pi_images.c_outbox, pi_data.OUTBOX, function(e: MouseEvent): void {
					if (!outbox_button.disable) app.dispatchEvent(new Event(pi_events.OUTBOXRELEASE));
				}, 93);
				outbox_button.disable = true;
			}*/
			
// Кнопка "эффекты"
			effect = new pi_toolbarButton(this, pi_images.c_effect, pi_data.EFFECTS, function(e: MouseEvent): void {
				app.dispatchEvent(new Event(pi_events.SHOWEFFECTDIALOG));
			}, 85);
			effect.visible = false;
			

// Кнопка "Сохранить в файл"

			function saveToJPG(cardbody: Object): void {
				var a_alert: pi_alert  = pj.alert(pi_data.MESSAGE, pi_data.JPGWAIT, null, false, null, function(): void {
					var a_bytes: ByteArray = cardbody.asBytes();
					a_alert.close(0, function(): void {
						pj.manager.saveJPG(a_bytes);
					});
				});
				a_alert.okButton.visible = false;
			}

			function toFileSave(e: Object=null): void {
				import MD5;
				var cardbody: Object = pj.isFullView?pj.fullScreen:pj.cardbody;
				if (cardbody.hasOwnProperty('asBytes')) saveToJPG(cardbody);
				else pj.dispatchEvent(new Event(pi_events.COLLECTFILERELEASE));
			}
			tofile = new pi_toolbarButton(this, pi_images.c_tofile, pi_data.COLLECTFILE, toFileSave, 90);
			tofile.visible = false;
			
			pj.addEventListener(pi_events.FULLVIEWPHOTOCOMPLETE, function(e: Event): void {
				if ((pj.editor.cardBody.maskInfo.price == 0) || 
					(pj.editor.cardBody.maskInfo.id != pj.fullScreen.maskInfo.id)) tofile.slideShow();
			})
			
// При загрузке фото в редакторе
			app.addEventListener(pi_events.EDITORPHOTOLOADCOMPLETE, function(e: ObjectEvent): void {
				if (!pj.editor.cardBody.maskInfo.price) {
//					if (!pj.isFullView) sendButtonsShow(true);
					tofile.slideShow();
				} else tofile.slideHide();
			});

			app.addEventListener(pi_events.COMPLETELOADCARD, function(e: ObjectEvent): void {
//				sendButtonsShow((pj.editor.cardBody.maskInfo.price == 0) && pj.isPhotoLoaded && !pj.isFullView);
				refrechVisibleEffect();
				eyeFull.slideVisible(e.object.maskInfo.id > 0);
			});

/*
			app.addEventListener(pi_events.CLEARPRICE, function(e: ObjectEvent): void {
				if (pj.isPhotoLoaded && 
					((e.object as Array).indexOf(pj.editor.cardBody.maskInfo.id) > -1)) {
					tofile.slideShow();
//					sendButtonsShow(true);
				}
			});
*/			
			
/*			app.addEventListener(pi_events.CARDSELECT, function(e: ObjectEvent): void { 
				// Если пользователь выбирает входящую открытку
				// Тогда скрываем кнопку "Отправить"
				sendButtonsShow(false);
			});*/

// Кнопка, "получить ссылку"
/*			link = new pi_toolbarButton(this, pi_images.c_link, pi_data.GETLINK, function(e: MouseEvent): void {
				pj.dispatchEvent(new Event(pi_events.LINKRELEASE));
			}, 100);
			link.visible = false;*/
			
			refreshButtons();
		}
		
		protected function refrechVisibleEffect(): void {
			effect.slideVisible(pj.editor.cardBody.permittedToFile && !pj.isFullView && (pj.editor.cardBody.maskInfo.id > 0));
		}
		
/*		protected function sendButtonsShow(isShow: Boolean): void {
			if (isShow) {
				link.slideShow();
			} else {
				link.slideHide();
			}
		}*/
		
		override protected function showFullView(): void {
			super.showFullView();
			refrechVisibleEffect();
			if (!pj.isFullView) {	// Если не на полный экран
				eyeFull.text = pi_data.FULLVIEWCARD;
				if (pj.editor.cardBody.maskInfo.price) tofile.slideHide();
//				else sendButtonsShow(true);
			} else {
				eyeFull.text = pi_data.FULLVIEWCARDCANCEL;
//				sendButtonsShow(false);
			}
		}
		
		private function stateEditAndPrice(): Boolean {
			return !pj.isFullView && (pj.editor.cardBody.maskInfo.price > 0);
		}
		
		override protected function updateBounds():uif_component {
			var res: uif_component = super.updateBounds();
			refreshButtons();
			return res;
		}
		
	}
}